// FiveDice Application. Chris Born, 2007

// BROKEN STILL WORKING ON IT

#import "FiveDiceController.h"

@implementation FDDefaultsController
- (id)init
{
	if(self = [super init])
	{
		NSLog(@"%s Defaults Controller", _cmd);
		NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithCapacity:14];
				
		[temp setObject:[NSNumber numberWithInt:MENU] forKey:@"SavedState"];
		[temp setObject:@"Player" forKey:@"PlayerOneName"];
		
		// TODO: Fix Yahtzee Count default
		[temp setObject:[[NSNumber alloc] initWithInt:0] forKey:@"YahtzeeCount"];
	
		[[NSUserDefaults standardUserDefaults] registerDefaults:temp];
		[temp release];
	}
	return self;
}
#pragma mark High Scores

int CompareDictionaries(id item1, id item2, void* context) // Sorting function
{
	NSNumber	*num1 = [item1 objectForKey:@"Score"];
	NSNumber	*num2 = [item2 objectForKey:@"Score"];
	return [num2 compare:num1];
}

- (NSMutableArray *)highScores
{
	return [[NSUserDefaults standardUserDefaults] arrayForKey:@"HighScores"];
}

- (NSMutableArray *)highScoresSorted
{	
	NSMutableArray		*array = [[NSMutableArray alloc] init];
	[array setArray:[self highScores]];	
	[array sortUsingFunction:CompareDictionaries context:nil];
	
	NSMutableArray		*temp = [[NSMutableArray alloc] init];
	if ([array count] > 20) {
		int i = 0;
		for(i = 0; i < 20; ++i)
		{
			[temp addObject:[array objectAtIndex:i]];
		}
		[array release];
		[temp autorelease];
		return temp;
	}
	[temp release];
	[array autorelease];
	return array;
}

- (void)setHighScores:(NSMutableArray *)newHighScores
{
	[[NSUserDefaults standardUserDefaults] setObject:newHighScores forKey:@"HighScores"];
	
}

- (void)clearHighScores
{
	[self setHighScores:nil];
}

- (BOOL)addedNewHighScore:(int)score forName:(NSString *)name
{
	NSMutableArray	*array = [[NSMutableArray alloc] init];
	[array setArray:[self highScoresSorted]];

	NSNumber	*scoreObj = [NSNumber numberWithInt:score];
	NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:scoreObj, @"Score", name, @"Name", nil];
	
	int count;
	if ([array count] < 20) {
		[array addObject:dict];
		[self setHighScores:array];
		[array release];
		return YES;
	} else {
		count = 20;
	}
	
	int i = 0;
	for(i = 0; i < count; ++i)
	{
		if ([[dict objectForKey:@"Score"] intValue] >= [[[array objectAtIndex:i] objectForKey:@"Score"] intValue]) {
			[array addObject:dict];
			[self setHighScores:array];
			[array release];
			return YES;			
		}
	}
	return NO;	
}


#pragma mark Default Accessors
- (int)savedState
{
	return [[NSUserDefaults standardUserDefaults] integerForKey:@"SavedState"];
}

- (void)setSavedState:(int)newSavedState
{
	[[NSUserDefaults standardUserDefaults] setInteger:newSavedState forKey:@"SavedState"];
}

- (NSString *)playerOneName
{
	return [[NSUserDefaults standardUserDefaults] objectForKey:@"PlayerOneName"];
}

- (void)setPlayerOneName:(NSString *)newPlayerOneName
{
	[[NSUserDefaults standardUserDefaults] setObject:newPlayerOneName forKey:@"PlayerOneName"];
}

- (NSString *)playerTwoName
{
	return [[NSUserDefaults standardUserDefaults] objectForKey:@"PlayerTwoName"];
}

- (void)setPlayerTwoName:(NSString *)newPlayerTwoName
{
	[[NSUserDefaults standardUserDefaults] setObject:newPlayerTwoName forKey:@"PlayerTwoName"];
}

- (NSArray *)scores
{
	return [[NSUserDefaults standardUserDefaults] arrayForKey:@"PersistenceScores"];
}

- (void)setScores:(NSArray *)newScores
{
	[[NSUserDefaults standardUserDefaults] setObject:newScores forKey:@"PersistenceScores"];
}

- (NSArray *)scoresSet
{
	return [[NSUserDefaults standardUserDefaults] arrayForKey:@"PersistenceScoresSet"];
}

- (void)setScoresSet:(NSArray *)newScoresSet
{
	[[NSUserDefaults standardUserDefaults] setObject:newScoresSet forKey:@"PersistenceScoresSet"];
}

- (int)rollCount
{
	return [[NSUserDefaults standardUserDefaults] integerForKey:@"RollCount"];
}

- (void)setRollCount:(int)newRollCount
{
	[[NSUserDefaults standardUserDefaults] setInteger:newRollCount forKey:@"RollCount"];
}

- (int)lastRoll
{
	return [[NSUserDefaults standardUserDefaults] integerForKey:@"LastRoll"];
}

- (void)setLastRoll:(int)newLastRoll
{
	[[NSUserDefaults standardUserDefaults] setInteger:newLastRoll forKey:@"LastRoll"];
}

- (int)turnCount
{
	return [[NSUserDefaults standardUserDefaults] integerForKey:@"TurnCount"];
}

- (void)setTurnCount:(int)newTurnCount
{
	[[NSUserDefaults standardUserDefaults] setInteger:newTurnCount forKey:@"TurnCount"];
}

- (int)diceTotal
{
	return [[NSUserDefaults standardUserDefaults] integerForKey:@"DiceTotal"];
}

- (void)setDiceTotal:(int)newDiceTotal
{
	[[NSUserDefaults standardUserDefaults] setInteger:newDiceTotal forKey:@"DiceTotal"];
}

- (int)selectedRow
{
	return [[NSUserDefaults standardUserDefaults] integerForKey:@"SelectedRow"];
}

- (void)setSelectedRow:(int)newSelectedRow
{
	[[NSUserDefaults standardUserDefaults] setInteger:newSelectedRow forKey:@"SelectedRow"];
}

- (int)lastSelectedRow
{
	return [[NSUserDefaults standardUserDefaults] integerForKey:@"LastSelectedRow"];
}

- (void)setLastSelectedRow:(int)newLastSelectedRow
{
	[[NSUserDefaults standardUserDefaults] setInteger:newLastSelectedRow forKey:@"LastSelectedRow"];
}

- (NSArray *)diceValues
{
	return [[NSUserDefaults standardUserDefaults] arrayForKey:@"PersistenceDiceValues"];
}

- (void)setDiceValues:(NSArray *)newDiceValues
{
	[[NSUserDefaults standardUserDefaults] setObject:newDiceValues forKey:@"PersistenceDiceValues"];
}

- (BOOL)wildcard
{
	return [[NSUserDefaults standardUserDefaults] boolForKey:@"Wildcard"];
}

- (void)setWildcard:(BOOL)newWildcard
{
	[[NSUserDefaults standardUserDefaults] setBool:newWildcard forKey:@"Wildcard"];
}

- (int)yahtzeeCount
{
	return [[NSUserDefaults standardUserDefaults] integerForKey:@"YahtzeeCount"];
}

- (void)setYahtzeeCount:(int)newYahtzeeCount
{
	[[NSUserDefaults standardUserDefaults] setInteger:newYahtzeeCount forKey:@"YahtzeeCount"];
}

- (BOOL)synchronize
{
  return [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)dealloc {
	BOOL unused = [[NSUserDefaults standardUserDefaults] synchronize];
	[super dealloc];
}

#pragma mark Score Persistence Accessors



@end